package main_temp;



import java.util.Iterator;

import ontprocessing_temp.OntProcessor;


import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;


import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

import edu.ege.thesis.nyt.TimesArticleSearch;
import edu.ege.thesis.nyt.TimesArticleSearchResult;
import edu.ege.thesis.nyt.TimesResultObject;

/**
 *
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	String nytId = null;
    	
    	String sparqlQueryString1= "PREFIX owl: <http://www.w3.org/2002/07/owl#> "+
    		    "   SELECT ?nytId"+
    		    "   WHERE {  "+
    		    "   ?nytId owl:sameAs <http://dbpedia.org/resource/Virginia> ."+
    		    "   FILTER regex(?nytId,\"nytimes\")." + 
    		    "        }";

    	
    	
    	
    		      Query query = QueryFactory.create(sparqlQueryString1);
    		      QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

    		     
    		      try {
    		    	  
    		          Iterator<QuerySolution> rs = qexec.execSelect() ;
    		          for ( ; rs.hasNext() ; )
    		          {
    		              QuerySolution soln = rs.next() ;
    		              nytId = soln.get("?nytId").toString();
    		              System.out.println(nytId);
    		   
    		          }
    		        } finally { qexec.close() ; }
    		      //  ResultSet results = qexec.execSelect();
    		      // ResultSetFormatter.out(System.out, results, query);   
    		      
    		    	String sparqlQueryString2= 
    		    			"PREFIX nyt: <http://data.nytimes.com/elements/> "+
    		    		    "   SELECT ?query"+
    		    		    "   WHERE {  <"+ nytId +
    		    		   // "   ?nytId "
    		    		    "> nyt:search_api_query ?query .}";
    		    	
    		      System.out.println(sparqlQueryString2);
    		      Query query2 = QueryFactory.create(sparqlQueryString2);
    		      QueryExecution qexec2 = QueryExecutionFactory.sparqlService("http://localhost:8080/openrdf-workbench/repositories/NYT/query", query2); 
    		      
    		      ResultSet results2 = qexec2.execSelect();
    		      ResultSetFormatter.out(System.out, results2, query2);      
    		      
    		      TimesArticleSearch mySearch = new TimesArticleSearch();
    		      
    		      
    		      mySearch.addFacetQuery("nytd_geo_facet", "Virginia"); //ask for articles from this desk
/*
    		      search.addExtra("begin_date", "19810101"); //begin and end dates

    		      search.addExtra("end_date", "19860101");
*/
    		      TimesArticleSearchResult r = mySearch.doSearch();
    		      TimesResultObject[] tro = r.getResults();
    		      for (int i = 0; i < tro.length; i++) {
    		    	 //   System.out.println("AUTHOR #" + i + ": " + tro[i].getAuthor());
    		    	    System.out.println("TITLE #" + i + ": " + tro[i].getTitle());
    		    	    System.out.println("ABSTRACT #" + i + ": " + tro[i].get_abstract());
    		    	    System.out.println("BODY #" + i + ": " + tro[i].getBody());
    		    	};
    		    	
    		    	
    		    	/*
    		    	
    		    	String sparqlQueryString3= 
    		    			"PREFIX void:<http://rdfs.org/ns/void#> "+
    		    		    "PREFIX dct:<http://purl.org/dc/terms/> " +
    		    		    "PREFIX prov:<http://www.w3.org/ns/prov#> " + 
    		    		    "   SELECT ?dataset"+
    		    		    "   WHERE {?dataset a void:Dataset." +
    		    		   // "   ?nytId "
    		    		    "?dataset prov:wasAttributedTo ?x.}";
    		      System.out.println(sparqlQueryString3);
      		      Query query3 = QueryFactory.create(sparqlQueryString3);
      		      QueryExecution qexec3 = QueryExecutionFactory.sparqlService("http://localhost:8080/openrdf-workbench/repositories/VoIDs/query", query3); 
      		      
      		      ResultSet results3 = qexec2.execSelect();
      		      ResultSetFormatter.out(System.out, results3, query3);  
      		      *//*
      		      String propuri="http://localhost:8080/openrdf-workbench/repositories/VoIDs#wasAttributedTo";
      		      OntProcessor ontProcessor = new OntProcessor("http://localhost:8080/openrdf-workbench/repositories/VoIDs");
      		      OntProperty ontProperty = (OntProperty) ontProcessor.getOntModel().getResource(propuri);
      		      */
    		     // qexec.close() ;
    		 //     qexec2.close();
    }
}
